<?php
/**
 * Created by PhpStorm.
 * User: Inhere
 * Date: 2015/3/4
 * Use : ...
 * File: AbstractIocFacade.php
 */
abstract class AbstractIocFacade extends AbstractFacade
{
    /**
     * 存储到容器的服务Id
     * @var [type]
     */
    static protected $key;

    /**
     * 容器名称
     * @var $name string
     */
    static protected $name;

    /**
     * 容器实例
     * @var $container object
     */
    static protected $container;

    static protected $instance = [];

    static public function getInstance()
    {
        $key = static::$key;

        if (!$key)
        {
            throw new \DLogicException('service Key not set!');
        }

        if (empty(static::$instance[$key]))
        {
            static::$instance[$key] = static::getContainer()->get($key);
        }

        return static::$instance[$key];
    }

    /**
     * Method to get property Container
     * @return  Container
     */
    static protected function getContainer()
    {
        return static::$container = Ioc::build(static::$name);
    }

    /**
     * Method to set property container
     * @param   Container $container
     * @return  void
     */
    static public function setContainer($container)
    {
        self::$container = $container;
    }

}